cmake_minimum_required(VERSION 3.16)

project(PixaFlow VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置 RPATH 相关选项
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "@executable_path/../Frameworks")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)
find_package(PkgConfig REQUIRED)
find_package(PNG REQUIRED)
pkg_check_modules(VULKAN REQUIRED vulkan)

add_subdirectory(effect_engine)

include_directories("./")
include_directories(${VULKAN_INCLUDE_DIRS})
include_directories(${PNG_INCLUDE_DIRS})

set(TS_FILES PixaFlow_zh_CN.ts)

set(PROJECT_SOURCES
        app/main.cpp
        app/mainwindow.cpp
        app/mainwindow.h
        app/mainwindow.ui
        ${TS_FILES}
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(PixaFlow
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET PixaFlow APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation

    qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()
    if(ANDROID)
        add_library(PixaFlow SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(PixaFlow
            ${PROJECT_SOURCES}
        )
    endif()

    qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    message(STATUS "Running on Linux")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    message(STATUS "Running on Windows")
    include_directories("D:/vcpkg_install/vcpkg/installed/x64-windows/include/libpng16")
    link_directories("$D:/vcpkg_install/vcpkg/installed/x64-windows/lib")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    message(STATUS "Running on macOS")
    set(VULKAN_SDK_PATH "/Users/neo/VulkanSDK/1.3.290.0")
    include_directories("/usr/local/include")
    include_directories("../")
    include_directories("${VULKAN_SDK_PATH}/macOS/include")
    include_directories("/opt/homebrew/opt/libpng/include")
    link_directories("/usr/local/lib")
    link_directories("${VULKAN_SDK_PATH}/macOS/lib")
    link_directories("/opt/homebrew/lib")
    link_directories("/opt/homebrew/opt/libpng/lib")
    add_compile_options(-fdeclspec)
    
    # 设置 Vulkan 库路径
    set(VULKAN_LIB "${VULKAN_SDK_PATH}/macOS/lib/libvulkan.1.dylib")
endif ()

target_link_libraries(PixaFlow PRIVATE 
    Qt${QT_VERSION_MAJOR}::Widgets 
    gpu_effect_engine
    PNG::PNG
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.PixaFlow)
endif()
set_target_properties(PixaFlow PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

# 创建 Frameworks 目录并复制 Vulkan 库
add_custom_command(TARGET PixaFlow POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:PixaFlow>/../Frameworks"
    COMMAND ${CMAKE_COMMAND} -E copy "${VULKAN_LIB}" "$<TARGET_FILE_DIR:PixaFlow>/../Frameworks/"
)

# 创建启动脚本
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    file(WRITE ${CMAKE_BINARY_DIR}/run.sh
        "#!/bin/bash\n"
        "export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH\n"
        "open ./PixaFlow.app\n"
    )
    execute_process(COMMAND chmod +x ${CMAKE_BINARY_DIR}/run.sh)
endif()

include(GNUInstallDirs)
install(TARGETS PixaFlow
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(PixaFlow)
endif()

# Copy the gpu_effect_engine library to the app bundle
add_custom_command(TARGET PixaFlow POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/PixaFlow.app/Contents/Frameworks"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${CMAKE_CURRENT_BINARY_DIR}/effect_engine/libgpu_effect_engine.dylib"
        "${CMAKE_CURRENT_BINARY_DIR}/PixaFlow.app/Contents/Frameworks/libgpu_effect_engine.dylib"
    # Copy compiled shaders directory
    COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/PixaFlow.app/Contents/Resources"
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${CMAKE_SOURCE_DIR}/effect_engine/shader_compiled"
        "${CMAKE_CURRENT_BINARY_DIR}/PixaFlow.app/Contents/Resources/shader_compiled"
)

# Update the install name of the library
add_custom_command(TARGET PixaFlow POST_BUILD
    COMMAND install_name_tool -change "@rpath/libgpu_effect_engine.dylib"
        "@executable_path/../Frameworks/libgpu_effect_engine.dylib"
        "${CMAKE_CURRENT_BINARY_DIR}/PixaFlow.app/Contents/MacOS/PixaFlow"
)
